#include <iostream>
using namespace std;

class Solution {
public:
    string replaceSpaces(string S, int length) {
        int count = 0;
        for (int i = 0; i < length;++i)
        {
            if(S[i] == ' ')
                ++count;
        }
        for (int i = length-1,j = length+2*count-1; i >= 0;)
        {
            if(S[i] != ' ')
            {
                S[j] = S[i];
                --i;
                --j;
            }
            else
            {
                S[j] = '0';
                S[j-1] = '2';
                S[j-2] = '%';
                --i;
                j -= 3;
            }
        }
        S[length + 2 * count] = '\0';
        return S;
    }
};

int main()
{
    Solution sol;
    cout << sol.replaceSpaces("Mr John Smith    ", 13) << endl;
    return 0;
}